package loupan

import (
	"hft/Http/Models/request"
	"hft/Http/Services/loupan"
	"hft/Http/Services/loupan/loupan_model"
	lou_serve_model "hft/Http/Services/loupan/loupan_model"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type LouPan struct{}

func (*LouPan) CreateLouPan(c *gin.Context) {
	var louPan *request.LouPan

	c.ShouldBindJSON(&louPan)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = loupan.LouPanService.CreateLouPan(louPan, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*LouPan) GetLouPanList(c *gin.Context) {
	var paginate request.LouPaPaginate
	c.ShouldBind(&paginate)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}
	data, err := loupan.NewLouPanService.GetLouPanList(&paginate, claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(data, c)
}

func (*LouPan) EditLouPan(c *gin.Context) {
	var louPan *request.LouPan

	c.ShouldBindJSON(&louPan)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = loupan.LouPanService.EditLouPan(louPan, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*LouPan) GetLouPanInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := loupan.LouPanService.GetLouPanInfo(id, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

func (*LouPan) Delete(c *gin.Context) {

	var IDS *request.IDS

	c.ShouldBindJSON(&IDS)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = loupan.LouPanService.Delete(IDS.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

// 获取驻守人员
func (*LouPan) GetGarrison(c *gin.Context) {
	var paginate lou_serve_model.GetGarrison
	c.ShouldBind(&paginate)
	if claims, err := tools.GetClaims(c); err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	} else {
		if list, err := loupan.NewLouPanService.GetGarrison(&paginate, claims.BaseClaims); err != nil {
			utils.FailWithMessage(err.Error(), c)
		} else {
			utils.OkWithData(list, c)
		}
	}
}

// 解除驻守
func (*LouPan) ReleaseBroker(c *gin.Context) {
	var data *loupan_model.ReleaseBody
	c.ShouldBindJSON(&data)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	if err = loupan.NewLouPanService.ReleaseBroker(data, claims.BaseClaims); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

// 添加驻守楼盘
func (*LouPan) AddBrokerToLouPan(c *gin.Context) {
	var data *loupan_model.AddBody
	c.ShouldBindJSON(&data)
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	if err = loupan.NewLouPanService.AddNewBroker(data, claims.BaseClaims); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.Ok(c)
	}
}

// 获取可以添加驻守的人员
func (*LouPan) FindBrokerNotGarrisonInThisLouPan(c *gin.Context) {
	var paginate loupan_model.GetGarrison
	c.ShouldBind(&paginate)
	if list, err := loupan.NewLouPanService.FindBrokerNotGarrisonInThisLouPan(&paginate); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(list, c)
	}

}

func (*LouPan) Phone400List(c *gin.Context) {
	lou_id, _ := strconv.Atoi(c.Query("lou_id"))
	if lou_id == 0 {
		utils.FailWithMessage(e.LouPanNotExist, c)
		return
	}
	conf, _ := loupan.NewLouPanService.FindLouPanPhone(uint(lou_id))
	utils.OkWithData(conf, c)
}

func (*LouPan) AddPhoneForLou(c *gin.Context) {
	var data *loupan_model.PhoneConfig
	if err := c.ShouldBindJSON(&data); err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	lou_id, _ := strconv.Atoi(c.Query("lou_id"))
	if lou_id == 0 {
		utils.FailWithMessage(e.LouPanNotExist, c)
		return
	}
	err := loupan.NewLouPanService.AddPhoneForLou(uint(lou_id), data)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

func (*LouPan) UpdateAudioPhone(c *gin.Context) {
	dn, _ := strconv.Atoi(c.Query("dn"))
	if dn == 0 {
		utils.FailWithMessage(e.PhoneNotRight, c)
		return
	}
	err := loupan.NewLouPanService.UpdateAudioPhone(dn)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

var LouPanController = new(LouPan)
